##################################################################
# /* Author: Gautam Nair and Nicholas Sambanis */
# /* Violence Exposure and Ethnic Identification: Evidence from Kashmir */
# /* Appendix Table 11: Correlation between preferences and identification  */

##################################################################

# change working directory here
# setwd("")

##################################################################
# loading packages
##################################################################
library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
library(plyr)
library(gridExtra)
library(dplyr)
library(scales)

##################################################################

rm(list=ls())

library(stargazer)

data.working <- read.csv("nei_kashmir_replication_dataset.csv")

data.temp <- data.working[data.working$group1_control==1,]

dep.var <- c("kashmir_status_india", "ind_pak_prefer_ind", "bonus_donation_india", "protest_peaceful_endorse", "protest_violent_endorse")

ind.var.1 <- c("identity_choice_indian_both")

ind.var.2 <- c("identity_rank_indian_rev")

list.ind.group <- list(ind.var.1, ind.var.2)

n.estimates <- 2*length(dep.var)

reg.models <-vector("list", n.estimates) 
se.models <-vector("list", n.estimates)
matrix <- vector("list", n.estimates)
x=0
for(i in 1:length(dep.var)) {
	for(y in 1:length(list.ind.group)){
		my.formula <- paste(dep.var[i],'~', paste(list.ind.group[[y]], collapse= ' + '))
		temp.model <- lm(my.formula, data=data.temp)
		temp.se <- coeftest(temp.model, vcov=vcovHC(temp.model,type="HC2"))[,2]
		x = x+1
		se.models[[x]] <- temp.se
		reg.models[[x]] <- temp.model
	}
}
	
varlabels <- c("Identity Indian or Indian and Kashmiri (1/0)", "Ranked Indian Identity (1-4)")
spectitle <- c("Correlation between Self-identification and Preferences (Control Group Respondents)")
outputfile <- c("tf_t_11_correlation_identity_preferences")
columnlabels <- c("Kashmir Status India (1/0)", "Ind-Pak: Prefer India (1/0)", "Donation: India (\\%)", "Peaceful Protests (1-5)", "Violent Protests (1-5)")
sepcolumns <- c(2,2,2,2,2)
cutoffs.star <- c(0.05, 0.01)
char.star = c("**", "***")
temptype <- c("latex", "text")
tempext <- c(".tex", ".txt")

for(q in 1:length(temptype)){
	temp.output <- paste(outputfile, tempext[q], sep="")

stargazer(reg.models[[1]],  reg.models[[2]], reg.models[[3]], reg.models[[4]], reg.models[[5]], reg.models[[6]], reg.models[[7]], reg.models[[8]],
reg.models[[9]], reg.models[[10]], 
          se=list(se.models[[1]],  se.models[[2]], se.models[[3]], se.models[[4]], se.models[[5]], se.models[[6]], se.models[[7]], se.models[[8]], se.models[[9]], se.models[[10]]),
         title= spectitle,
          out=temp.output,
          no.space=TRUE, 
          model.numbers= TRUE,
          align=FALSE,
        covariate.labels=varlabels,
          header= FALSE,
          font.size="scriptsize",
          model.names=TRUE,
          digits=2,
          omit.stat = c("rsq", "f","adj.rsq"),
         column.labels  = columnlabels,
         column.separate =sepcolumns,
          dep.var.caption  = "",
          dep.var.labels.include=FALSE,
         df=FALSE,
         omit.table.layout="n",
                  star.cutoffs = cutoffs.star,
     star.char = char.star,
	column.sep.width = "0.5pt",
         notes.align=c("l"),
         type=temptype[q],
notes.label = "",
# notes        = specnotes,
  notes.append = TRUE
) 
}
